<#assign tit>K-Yoga合同销售</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function seldj() {
    var fm = getEl("fm_input");
    var url = "getEarnestList?type=7";
    if(fm.memberCode.value=="") {
      window.alert("请先输入会员卡号!");
      return;
    }
    url += "&memberCode="+fm.memberCode.value;
    openWin(url, 620, 400);
  }
  function seldjOk(vv) {
    var ss = vv.split(",");
    var fm = getEl("fm_input");
    fm.earnestId.value = ss[0];
    fm.earnestAmount.value = ss[1];
    chgfact();
  }

  function getminfo(c) {
    if(c!="") {
      getJson("getMemberKy?cardCode="+c, function(data){
        if(data.ret) {
          var mb = data.data;
          var fm = getEl("fm_input");
          fm.memberCardCode.value = mb.card;
          fm.memberCode.value = mb.code;
          fm.memberName.value = mb.name;
          fm.contractTime.value = mb.contractEnd;
          fm.leftAmount.value = mb.kyFactAmount?(formatMoney(mb.kyFactAmount)+"+"+formatMoney(mb.kyPresentAmount)):"";
          fm.kyContractTime.value = mb.kyContractEnd;
          fm.cardCode.value = mb.kyogaCard;
          if(fm.comboCode.value!="") chgcb();
        } else {
          window.alert(data.msg);
          cldc();
        }
      });
    } else {
      cldc();
    }
  }

  function cldc() {
    var fm = getEl("fm_input");
    fm.memberCardCode.value = "";
    fm.memberCode.value = "";
    fm.memberName.value = "";
    fm.contractTime.value = "";
    fm.leftAmount.value = "";
    fm.kyContractTime.value = "";
    fm.cardCode.value = "";
  }

  function chgcb() {
    var fm = getEl("fm_input");
    if(fm.comboCode.value=="") {
      fm.factAmount.value = "";
      fm.presentAmount.value = "";
      fm.payAmount.value = "";
      fm.beginTime.value = "";
      fm.endTime.value = "";
      return;
    }

    var sl = fm.comboCode.options[fm.comboCode.selectedIndex];
    fm.factAmount.value = formatMoney(sl.getAttribute("cm"));
    fm.presentAmount.value = formatMoney(sl.getAttribute("pm"));
    fm.payAmount.value = fm.factAmount.value;
    var valids = parseInt(sl.getAttribute("vn"));
    var validUnit = sl.getAttribute("vt");

    var dt = new Date();
    if(fm.kyContractTime.value!="" && fm.kyContractTime.value>getDateStr(dt)) {
      dt = parseDate(fm.kyContractTime.value);
      dt.addDay(1);
    }
    fm.beginTime.value = getDateStr(dt);
    if(validUnit=='01')
      dt.addDay(valids);
    else if(validUnit=='02')
      dt.addWeek(valids);
    else if(validUnit=='03')
      dt.addMonth(valids);
    else if(validUnit=='04')
      dt.addYear(valids);
    fm.endTime.value = getDateStr(dt);
  }

  function chgfact() {
    var fm = getEl("fm_input");
    if(fm.factAmount.value=="") {
      fm.payAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.factAmount.value);
    if(isNaN(m1)) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    fm.factAmount.value = formatMoney(m1);
    fm.payAmount.value = formatMoney(m1-m2);
  }

  function selCard() {
    openWin("getKyCardList", 500, 400);
  }
  function selCardOk(vv) {
    getEl("fm_input").cardCode.value = vv;
  }
  function chkCard(ipt) {
    if(ipt.value!="") {
      getJson("getKyCardNew?cardCode="+ipt.value, function(data){
        if(data.ret) {
          ipt.value = data.data.cardCode;
        } else {
          window.alert(data.msg);
        }
      });
    }
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(fm.memberCode.value=="") {
      window.alert("请输入会员卡号");
      return false;
    }

    var mm = fm.payAmount.value;
    post(fm, function(){
      openWin("getPay?money="+mm, 500, 400);
      return false;
    });
    return false;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm(fm.action, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) getEl("fm_input").reset(); //window.location = "print?id="+data.data;
  }
</script>
<div style="width:600px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <div class="kbox">
      <table class="input w4">
        <tr>
          <th width="85">会员卡号</th>
          <td width="160"><input type="text" name="memberCardCode" value="" onchange="getminfo(this.value);" maxlength="20"></td>
          <th width="85">会员编号</th>
          <td width="160"><input type="text" name="memberCode" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>会员姓名</th>
          <td><input type="text" name="memberName" value="" class="disable" readonly></td>
          <th>合同终止日</th>
          <td><input type="text" name="contractTime" value="" maxlength="10" class="disable" readonly></td>
        </tr>
        <tr>
          <th>剩余金额</th>
          <td><input type="text" name="leftAmount" value="" class="disable" readonly></td>
          <th>有效期</th>
          <td><input type="text" name="kyContractTime" value="" maxlength="10" class="disable" readonly></td>
        </tr>
        <tr style="display:none;">
          <th>定金编号</th>
          <td><input type="text" name="earnestId" value="" style="width:90px;" class="disable" readonly>
            <input type="button" class="opwin" value="..." onclick="seldj();"/></td>
          <th>定金金额</th>
          <td><input type="text" name="earnestAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>选择套餐</th>
          <td colspan="3"><select name="comboCode" onchange="chgcb();" class="w2" req="套餐"><option value="">--请选择--</option><#if comboList??><#list comboList as t><option value="${t.code!}" cm="${t.chargeAmount!}" pm="${t.presentAmount!}" vn="${t.valids!}" vt="${t.validUnit!}">${t.code!} - ${t.chName!}</option></#list></#if></select></td>
        </tr>
        <tr>
          <th>购买金额</th>
          <td><input type="text" name="factAmount" value="" req="购买金额"></td>
          <th>赠送金额</th>
          <td><input type="text" name="presentAmount" value="" req="赠送金额"></td>
        </tr>
        <tr>
        <tr>
          <th>开始日期</th>
          <td><input type="text" name="beginTime" value="" class="date disable" maxlength="10" readonly onfocus="if(!this.readOnly)cal(this);"></td>
          <th>结束日期</th>
          <td><input type="text" name="endTime" value="" class="date disable" maxlength="10" readonly></td>
        </tr>
        </tr>
        <tr>
          <th>K-Yoga卡号</th>
          <td><input type="text" name="cardCode" value="" onchange="chgcard(this);" maxlength="20" style="width:90px;" req="K-Yoga卡号">
            <input type="button" id="bt_sc" class="opwin" value="..." onclick="selCard();"/></td>
          <th>应收金额</th>
          <td><input type="text" name="payAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>备注</th>
          <td colspan="3"><textarea name="note"></textarea></td>
        </tr>
      </table>
    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>
</@page.html>